package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /***
     * 违纪操作
     */
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable("id") Integer id,@PathVariable("score")Short score){
        //调用Service层执行修改
        studentService.updateViolation(id,score);
        return Result.success();
    }

    /***
     * 删除学员信息
     */
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids){
        //调用Service层实现删除
        studentService.delete(ids);
        return Result.success();
    }

    /***
     * 修改学员信息
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        //调用Service需改学员信息
        studentService.update(student);
        return Result.success();
    }

    /***
     * 需改回显，根据ID查询
     */
    @GetMapping("/{id}")
    public Result getStudentInfo(@PathVariable Integer id){
        //调用Service实现查询
        Student student = studentService.getStudentInfo(id);
        return Result.success(student);
    }

    /***
     * 新增学员信息
     */
    @PostMapping
    public Result add(@RequestBody Student student){
        //调用Service层实现新增
        studentService.add(student);
        return Result.success();
    }

    /***
     * 分页集合查询
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        //调用Service层实现查询
        PageResult pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

}
